package co.edu.javeriana.konigsberg.xavier.servicio;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.ejb.Stateful;
import javax.ejb.Stateless;

import co.edu.javeriana.konigsberg.xavier.dao.desc.AreaGestionDAOLocal;
import co.edu.javeriana.konigsberg.xavier.dto.AreaGestionDTO;
import co.edu.javeriana.konigsberg.xavier.entidad.Areagestion;

/**
 * Session Bean implementation class AreaGestionServicio
 */
@Stateless
public class AreaGestionServicio implements AreaGestionServicioRemote, AreaGestionServicioLocal {
	
	@EJB
	private AreaGestionDAOLocal areaGestionDAO;
	
	private static Map<BigInteger, AreaGestionDTO> mapaAreasGestion;
	
	private List<AreaGestionDTO> areaGestionDTOs;
	
    /**
     * Default constructor. 
     */
    public AreaGestionServicio() {
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public List<AreaGestionDTO> obtenerLista(){
    	if(mapaAreasGestion == null){
    		mapaAreasGestion = new HashMap<BigInteger, AreaGestionDTO>();
    		areaGestionDTOs = new ArrayList<AreaGestionDTO>();
			List<Areagestion> areasgestion = areaGestionDAO.obtenerLista();
			for (Areagestion areagestion : areasgestion) {
				AreaGestionDTO areaGestionDTO = getDTO(areagestion);
				areaGestionDTOs.add(areaGestionDTO);
				mapaAreasGestion.put(areaGestionDTO.getIdAreaGestion(), areaGestionDTO);
			}
    	}
		
		return areaGestionDTOs;
	}

	public AreaGestionDTO getDTO(Object entidad) {
		Areagestion areagestion = (Areagestion)entidad;
		AreaGestionDTO areaGestionDTO = new AreaGestionDTO();
		areaGestionDTO.setIdAreaGestion(areagestion.getIdAreaGestion());
		areaGestionDTO.setNombreAreaGestion(areagestion.getNombreAreaGestion());
		return areaGestionDTO;
	}

	@Override
	public AreaGestionDTO obtenerAreaGestionPorId(BigInteger id) {
		if(mapaAreasGestion == null){
			obtenerLista();
		}
		return mapaAreasGestion.get(id);
	}

}
